﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using GtsServer.Manage.Controller;
using GtsServer.Model;
using GtsServer.Common;

namespace GtsServer.Manage.IISPage.Ajax
{
    /// <summary>
    /// TaskRobotSettingAjax 的摘要说明
    /// </summary>
    public class TaskRobotSettingAjax : IHttpHandler
    {
        TaskRobotSettingController controller = new TaskRobotSettingController();
        public void ProcessRequest(HttpContext context)
        {
            string type = string.Empty;
            if (context.Request["action"] != null)
            {
                type = context.Request["action"];

                switch (type)
                {
                    case "GetModelByTaskID":
                        GetModelByTaskID(context);
                        break;
                    case "Edit":
                        Edit(context);
                        break;
                    default:
                        break;
                }
            }
        }

        private void Edit(HttpContext context)
        {
            int id = Convert.ToInt32(context.Request["id"]);
            int taskID = Convert.ToInt32(context.Request["taskID"]);
            int robotAddTime = Convert.ToInt32(context.Request["robotAddTime"]);
            int scoreEditTime = Convert.ToInt32(context.Request["scoreEditTime"]);
            int minScore = Convert.ToInt32(context.Request["minScore"]);
            int maxScore = Convert.ToInt32(context.Request["maxScore"]);
            int robotMinIndex = Convert.ToInt32(context.Request["robotMinIndex"]);
            int robotMaxIndex = Convert.ToInt32(context.Request["robotMaxIndex"]);
            int robotMinCount = Convert.ToInt32(context.Request["robotMinCount"]);
            int robotMaxCount = Convert.ToInt32(context.Request["robotMaxCount"]);
            int robotInitMinScore = Convert.ToInt32(context.Request["robotInitMinScore"]);
            int robotInitMaxScore = Convert.ToInt32(context.Request["robotInitMaxScore"]);

            TaskRobotSetting model = new TaskRobotSetting()
            {
                ID = id,
                TaskID = taskID,
                RobotAddTime = robotAddTime,
                ScoreEditTime = scoreEditTime,
                MinScore = minScore,
                MaxScore = maxScore,
                RobotMinIndex = robotMinIndex,
                RobotMaxIndex = robotMaxIndex,
                RobotMinCount = robotMinCount,
                RobotMaxCount = robotMaxCount,
                RobotInitMinScore = robotInitMinScore,
                RobotInitMaxScore = robotInitMaxScore
            };

            context.Response.Write(controller.Edit(model));
        }

        private void GetModelByTaskID(HttpContext context)
        {
            int taskID = Convert.ToInt32(context.Request["taskID"]);
            TaskRobotSetting model = controller.GetModelByTaskID(taskID);

            string json = JsonHelper.ToJson(model ?? new TaskRobotSetting());
            context.Response.Write(json);
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}